<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';

$myPdo = DbUtils::createPdoInst();

$post_id = MiscUtils::getParam('news', 0);

$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $post_id);
$opts = new stdClass();
$opts->select_expr = 'Distinct t.tag_ids';
$tag_str = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, NULL, NULL, 1, 1, $opts)->d[0]->tag_ids;

$tags = explode('|', $tag_str);
$ids = array();
foreach ($tags as $tag) {
  if (is_numeric($tag)) {
    $ids[] = $tag;
  }
}

$SIZE = 10;
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.id');
$queue = MiscUtils::getParam('q', 'DESC');
$cond_vals = new stdClass();
$cond = '';
$vals = array();

foreach ($ids as $idx => $id) {
  $cond .= 't.tag_ids like :v' . ($idx + 1);
  if ($idx < (count($ids) - 1)) {
    $cond .= ' OR ';
  }
  $vals[':v' . ($idx + 1)] = '%|' . $id . '|%';
}
$cond_vals = new stdClass();
$cond_vals->c = $cond;
$cond_vals->v = $vals;
$posts = array();
$opts = new stdClass();
$opts->select_expr = 'Distinct t.id, t.title';
if ($cond_vals->c != '') {
  $relas = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals, $order, $queue, $page, $SIZE, $opts)->d;
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<style>
.title {
  display: table-cell;
  width: 189px;
  height: 29px;
  padding-left: 5px;
  vertical-align: middle;
  background: url(images/titlebg.gif);
  font: bold 14px Arial;
  color: #fff;
}
#scroller {
  margin: 0px;
  list-style: none;
  padding: 8px;
}

#scroller li {
  width: 100%;
  vertical-align: middle;
  background: url(images/bullet.gif) no-repeat left center;
  clear: both;
  font: 12px/1.5 Verdana, Arial, Helvetica, sans-serif, "宋体";
  line-height: 18px;
  padding: 2px 0 2px 8px;
  height: 18px;
}

#scroller a {
  color: #333;
  text-decoration: none;
}
#scroller a:hover{
  text-decoration: underline;
}
</style>
</head>

<body>

<div style="border: 1px solid #ddd;">
  <div class="title">Related Articles</div>
  <ul id="scroller">
    <?php foreach ($relas as $idx => &$item) { ?>
    <li><a href="#" title="<?php echo $item->title; ?>" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . NEWS_ID . "&script=index_detail&news=$item->id"; ?>';"><?php echo MiscUtils::getShortName($item->title, 12); ?><a></li>
    <?php } ?>
    <?php if (count($relas) == 0) { ?>
    <li>No Result Found</li>
    <?php } ?>
  </ul>
</div>
</body>
</html>
